﻿using Aker.DevTeam;
using Extensions;
using System;
using System.Data;

namespace Aker.Cikislar
{
    public partial class YeniIrsaliye : XForm
    {

        int CikisId;
        public string IrsaliyeNo;
        public YeniIrsaliye(int cikisId)
        {
            InitializeComponent();
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.CikisId = cikisId;
        }

        private void btnKaydet_Click(object sender, EventArgs e)
        {
            if (txtSeri.Text == "" || txtSiraNo.Text == "")
            {
                ShowError("Sıra/Seri doldurulmalı !");
                return;
            }

            if (AppSession.UserInfo.UserId == 24)
                IrsaliyeNo = "2-";
            else
                IrsaliyeNo = "1-";
            IrsaliyeNo += txtSeri.Text + "-" + txtSiraNo.Text;
            Data.DM.ExecuteNonQuery("UpdIrsaliyeNo", CikisId, IrsaliyeNo.Trim(), gleCari.EditValue.ToIntNN(), txtCariAdres.Text.Trim(), gleSevkCari.EditValue.ToIntNN(), txtSevkAdres.Text.Trim(), txtAciklama.Text.Trim(), deSevkiyatTarihi.DateTime, deTanzimTarihi.DateTime);

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void SeriNo_Load(object sender, EventArgs e)
        {
            DataRow dr = Data.DM.ExecuteDataTable("GetIrsaliyeHeader", AppSession.UserInfo.UserId, CikisId).Rows[0];
            string iNo = dr["iNo"].ToStringNN();
            int cariId = dr["CariId"].ToIntNN();
            string[] vals = iNo.Split('-');
            txtSeri.Text = vals[1];
            txtSiraNo.Text = (vals[2].ToIntNN() + 1).ToString();

            DateTime now = DateTime.Now;
            deTanzimTarihi.DateTime = now;
            deSevkiyatTarihi.DateTime = now;

            DevExpress.XtraEditors.Repository.RepositoryItemGridLookUpEdit gle = Utility.GetAutoCompGLE(Data.DB.Customer, "Brand", "CustomerId");
            gle.View.EnableComplexFiltering("Brand");

            gleCari.Properties.Assign(gle);
            gleSevkCari.Properties.Assign(gle);

            if (cariId > 0)
            {
                gleCari.EditValue = cariId;
                gleSevkCari.EditValue = cariId;
            }
        }

        private void gleCari_EditValueChanged(object sender, EventArgs e)
        {
            int id = gleCari.EditValue.ToIntNN();
            if (id > 0)
                txtCariAdres.Text = Data.DM.ExecuteScalar("GetCariAdres", id).ToStringNN();
            else
                txtCariAdres.Text = "";
        }

        private void gleSevkCari_EditValueChanged(object sender, EventArgs e)
        {
            int id = gleSevkCari.EditValue.ToIntNN();
            if (id > 0)
                txtSevkAdres.Text = Data.DM.ExecuteScalar("GetCariAdres", id).ToStringNN();
            else
                txtSevkAdres.Text = "";
        }

        private void btnCariNull_Click(object sender, EventArgs e)
        {
            gleCari.EditValue = null;
        }

        private void btnCariAdresNull_Click(object sender, EventArgs e)
        {
            txtCariAdres.Text = "";
        }

        private void btnSevkCariNull_Click(object sender, EventArgs e)
        {
            gleSevkCari.EditValue = null;
        }

        private void btnSevkAdresiNull_Click(object sender, EventArgs e)
        {
            txtSevkAdres.Text = "";
        }

        private void btnAciklamaNull_Click(object sender, EventArgs e)
        {
            txtAciklama.Text = "";
        }
    }
}
